package com.example.a2022090658

import android.R
import android.annotation.SuppressLint
import android.content.ClipboardManager
import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.TextView
import androidx.core.app.ActivityCompat
import androidx.core.widget.addTextChangedListener
import com.example.a2022090658.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    private val EncoderAndEncoder = EncAndDec()

    private fun encrypt(plainText: String, key: String): String {
        var usedKey: String? = ""
        if (key.length <16){
            usedKey = key.padEnd(16,'a' + key.length);
        }
        else if(key.length >16){
            var startPos = 0
            var endPos = key.length - 1
            for (loopNum in 0..15){
                if (startPos <= endPos){
                    usedKey.plus(key[startPos])
                    startPos++
                }
                else{
                    usedKey.plus(key[endPos])
                    endPos--
                }
            }
        }
        else {
            usedKey = key
        }
        if (usedKey != null){
            println("key: $usedKey")
            return EncoderAndEncoder.EncText(plainText, usedKey)
        }
        else{
            return EncoderAndEncoder.EncText(plainText)
        }
    }

    private fun decrypt(cipherText: String, key: String): String {
        var usedKey: String? = ""
        if (key.length <16){
            usedKey = key.padEnd(16,'a' + key.length);
        }
        else if(key.length >16){
            var startPos = 0
            var endPos = key.length - 1
            for (loopNum in 0..15){
                if (startPos <= endPos){
                    usedKey.plus(key[startPos])
                    startPos++
                }
                else{
                    usedKey.plus(key[endPos])
                    endPos--
                }
            }
        }
        else {
            usedKey = key
        }
        if (usedKey != null){
            return EncoderAndEncoder.DecText(cipherText, usedKey)
        }
        else{
            return EncoderAndEncoder.DecText(cipherText)
        }

    }
    private val  REQUEST_EXTERNAL_STORAGE = 1;
    private val PERMISSIONS_STORAGE = arrayOf("android.permission.READ_EXTERNAL_STORAGE")

    private fun CopyTextIntoCpilboard(text: String){
        if (ActivityCompat.checkSelfPermission(this, "android.permission.READ_EXTERNAL_STORAGE") != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
        }

        val clipboard = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
        val clip = android.content.ClipData.newPlainText("text", text)
        val alterDialog = android.app.AlertDialog.Builder(this)
        alterDialog.setTitle("操作信息")
        alterDialog.setMessage("成功复制到剪切板")
        alterDialog.setPositiveButton("确定"){dialog, which ->
            dialog.dismiss()
        }
        alterDialog.show()
        clipboard.setPrimaryClip(clip)
    }
    private fun GetCopyedData(){
        if (ActivityCompat.checkSelfPermission(this, "android.permission.READ_EXTERNAL_STORAGE") != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
        }
        val clipboard = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
        val clipData = clipboard.primaryClip
        if (clipData != null){
            val item = clipData.getItemAt(0)
            val text = item.text
            binding.plainOrCiphe.setText(text)
        }
        if (clipData == null){
            val alterDialog = android.app.AlertDialog.Builder(this)
            alterDialog.setTitle("操作信息")
            alterDialog.setMessage("剪切板为空")
            alterDialog.setPositiveButton("确定"){dialog, which ->
                dialog.dismiss()
            }
            alterDialog.show()
        }
    }

    private fun clearText(){
        binding.plainOrCiphe.setText("")
    }
    @SuppressLint("ClickableViewAccessibility")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.getData.setOnClickListener{GetCopyedData()}
        binding.enc.setOnClickListener {
            val Text = binding.plainOrCiphe.text.toString()
            val key = binding.key.text.toString()
            val cipherText = encrypt(Text, key)
            if(binding.CopyIt.isChecked){
                CopyTextIntoCpilboard(cipherText)
            }
            binding.show.setText(cipherText)
        }
        binding.dec.setOnClickListener{
            val Text = binding.plainOrCiphe.text.toString()
            val key = binding.key.text.toString()
            val plainText = decrypt(Text, key)
            if(binding.CopyIt.isChecked){
                CopyTextIntoCpilboard(plainText)
            }
            binding.show.setText(plainText)
        }
        binding.clear.setOnClickListener { clearText() }
    }

    /**
     * A native method that is implemented by the 'a2022090658' native library,
     * which is packaged with this application.
     */
    external fun stringFromJNI(): String

    companion object {
        // Used to load the 'a2022090658' library on application startup.
        init {
            System.loadLibrary("a2022090658")
        }
    }
}